﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Caro
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DrawBoard();

        }

        public enum CaroTurn { None, X, O };
        public enum CaroMark { None, X, O };

        public CaroTurn turn = CaroTurn.X;
        public CaroMark[,] matrix = new CaroMark[15, 15];

        public void myInit()
        {
            for (int i = 0; i < 15; i++)
                for (int j = 0; j < 15; j++)
                    matrix[i, j] = CaroMark.None;
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ClearBoard();
        }
        public void ClearBoard()
        {
            cvBoard.Children.Clear();

            for (int i = 0; i < 15; i++)
                for (int j = 0; j < 15; j++)
                    matrix[i, j] = CaroMark.None;

            DrawBoard();
        }
        public void DrawBoard()
        {
            for (int i = 0; i < 15; i++)
            {
                Line l1 = new Line();
                l1.X1 = 5 + i * 40; l1.Y1 = 5;
                l1.X2 = 5 + i * 40; l1.Y2 = 565;
                l1.StrokeThickness = 2;
                l1.Stroke = Brushes.Black;
                cvBoard.Children.Add(l1);

                Line l2 = new Line();
                l2.Y1 = 5 + i * 40; l2.X1 = 5;
                l2.Y2 = 5 + i * 40; l2.X2 = 565;
                l2.StrokeThickness = 2;
                l2.Stroke = Brushes.Black;
                cvBoard.Children.Add(l2);
            }
        }
        public void DrawO(Point p)
        {
            double _y = Math.Round(p.Y / 40);
            double _x = Math.Round(p.X / 40);

            if (matrix[(int)_x, (int)_y] != CaroMark.None) return;

            turn = CaroTurn.X;
            matrix[(int)_x, (int)_y] = CaroMark.O;

            Ellipse e = new Ellipse();
            e.Width = 26;
            e.Height = 26;
            e.Stroke = Brushes.Blue;
            e.StrokeThickness = 4;
            double y = _y * 40 - 8;
            double x = _x * 40 - 8;
            Canvas.SetTop(e, y);
            Canvas.SetLeft(e, x);
            cvBoard.Children.Add(e);
        }

        public void DrawX(Point p)
        {
            Line l1 = new Line();
            Line l2 = new Line();

            double _y = Math.Round(p.Y / 40);
            double _x = Math.Round(p.X / 40);

            if (matrix[(int)_x, (int)_y] != CaroMark.None) return;

            turn = CaroTurn.O;
            matrix[(int)_x, (int)_y] = CaroMark.X;

            double y = _y * 40 - 10;
            double x = _x * 40 - 10;

            l1.X1 = x+5; l1.Y1 = y+5;
            l1.X2 = x + 25; l1.Y2 = y + 25;
            l1.StrokeThickness = 4;
            l1.Stroke = Brushes.Purple;
            

            l2.X2 = x + 5; l2.Y1 = y + 5;
            l2.X1 = x + 25; l2.Y2 = y + 25;
            l2.StrokeThickness = 4;
            l2.Stroke = Brushes.Purple;

            cvBoard.Children.Add(l1);
            cvBoard.Children.Add(l2);

            turn = CaroTurn.O;
            matrix[(int)_x, (int)_y] = CaroMark.X;

        }

        private void cvBoard_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(cvBoard);
            if (turn == CaroTurn.X) DrawX(p);
            else DrawO(p);
            CaroTurn winner = CheckWin();
            if (winner != CaroTurn.None)
            {
                MessageBox.Show(winner.ToString() + " wins!");
                ClearBoard();
            }
        }

        private CaroTurn CheckWin()
        {
            
            for (int i = 0; i < 15; i++)
            {
                string s1 = "";
                string s2 = "";
                for (int j = 0; j < 15; j++)
                {
                    if (matrix[i, j] == CaroMark.None) s1 = s1 + " ";
                    else s1 = s1 + matrix[i, j].ToString();

                    if (matrix[j, i] == CaroMark.None) s2 = s2 + " ";
                    else s2 = s2 + matrix[j, i].ToString();
                }
                if (s1.Contains("XXXXX") || s2.Contains("XXXXX")) return CaroTurn.X;
                if (s1.Contains("OOOOO") || s2.Contains("OOOOO")) return CaroTurn.O;
            }

            for (int i = 0; i < 30; i++)
            {
                string s1 = "";
                string s2 = "";
                for (int j = 0; j < 15; j++)
                {
                    int k = i - j; 
                    if ((k >= 0) && (k < 15))
                    {
                        if (matrix[j, k] == CaroMark.None) s1 = s1 + " ";
                        else s1 = s1 + matrix[j, k].ToString();

                        if (matrix[14-j, k] == CaroMark.None) s2 = s2 + " ";
                        else s2 = s2 + matrix[14-j, k].ToString();
                    }
                }

                if (s1.Contains("XXXXX") || s2.Contains("XXXXX")) return CaroTurn.X;
                if (s1.Contains("OOOOO") || s2.Contains("OOOOO")) return CaroTurn.O;
            }
                return CaroTurn.None;
        }
    }
}
